package com.cn.main.framework.proxy;

import com.cn.main.framework.annotation.ZxFeign;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * user : chenyuncong
 * create_time : 2022/9/3
 */
public class HttpProxy implements InvocationHandler {
    private Class<?> interfaceClass;
    private String serviceName;
    private RestTemplate restTemplate;

    public HttpProxy(Class<?> interfaceClass, String serviceName) {
        this.interfaceClass = interfaceClass;
        this.serviceName = serviceName;
    }

    public HttpProxy(Class<?> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    public HttpProxy(Class<?> interfaceClass, String serviceName, RestTemplate restTemplate) {
        this.interfaceClass = interfaceClass;
        this.serviceName = serviceName;
        this.restTemplate = restTemplate;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        System.out.println(proxy);
        ZxFeign feign = method.getDeclaringClass().getAnnotation(ZxFeign.class);
        if(!Objects.isNull(feign)){
            System.out.println(method);
            System.out.println(method.getName());
            System.out.println(method.getParameterTypes());
//            System.out.println(args);
            for (Object o: args){
                System.out.println(o);
            }
            System.out.println("代理T类型做一些事情");
            if(!Objects.isNull(restTemplate)){
                ResponseEntity<String> html = restTemplate.getForEntity("http://baidu.com",String.class);
                System.out.println(html.getBody());
            }

            return "返回了:"+args[0];
        }
        return null;
    }
}
